package cn.lijiajia3515.cairo.captcha.image.graph;

import cn.hutool.core.util.RandomUtil;
import cn.lijiajia3515.cairo.captcha.image.ImageCaptcha;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 委派 验证码 处理器
 */
public class DelegatingGraphImageHandler implements GraphImageHandler {
	private final List<? extends GraphImageHandler> handlers;

	public DelegatingGraphImageHandler() {
		handlers = Stream.of(
			new PngGraphCaptchaImageHandler(),
			new GifGraphImageCaptchaHandler()
		).collect(Collectors.toUnmodifiableList());
	}

	public DelegatingGraphImageHandler(List<? extends GraphImageHandler> captcha) {
		this.handlers = captcha;
	}

	@Override
	public ImageCaptcha.ImageCaptchaDetail getDetail(GraphImageArgs args) {
		return handlers.get(RandomUtil.randomInt(handlers.size())).getDetail(args);
	}
}
